<?php

class SlideController extends Controller
{
  public $layout='//layouts/main_page';

  public function filters()
  {
    return array(
      'accessControl', // perform access control for CRUD operations
    );
  }

  public function accessRules()
  {
    return array(
      array('allow',  // allow all users to perform 'index' and 'view' actions
        'actions'=>array('index','view'),
        'users'=>array('*'),
      ),
      array('allow', // allow authenticated user to perform 'create' and 'update' actions
        'actions'=>array('create','update','list','delete','setDefault','upPhoto','downPhoto','updateStatus'),
        'users'=>array('@'),
      ),
      array('deny',  // deny all users
        'users'=>array('*'),
      ),
    );
  }

  function init(){
    if(isset($_POST['SESSION_ID'])){
      $session=Yii::app()->getSession();
      $session->close();
      $session->sessionID = $_POST['SESSION_ID'];
      $session->open();
    }
  }

  public function actionList($modelName='index', $modelId=0)
  {
    $modelId = ($modelId!="") ? $modelId : 0;
    
    $condition = 'model_name=:modelName';
    $params = array(':modelName' => $modelName);
    if ($modelId != null) {
      $condition .= ' AND model_id=:model_id';
      $params[':model_id'] = $modelId;
    }
    
    
    $dataProvider = new CActiveDataProvider('Slide',
      array(
        'criteria'=>array(
          'condition' => $condition,
          'params' => $params,
          'order'=> 'display_order ASC',
        )
      ));

    $this->render('list',compact('dataProvider','modelName', 'modelId'));
  }

  public function actionCreate($modelName='index', $modelId=0)
  {
    $modelId = ($modelId!="") ? $modelId : 0;
    
    $model=new Slide();
    $model->model_name = $modelName;
    $model->model_id = $modelId;
    
    if(isset($_POST['imagePhoto'])){
      $model->savePhoto();
    }

    
    $this->render('create',compact('modelName', 'modelId'));
  }

  public function actionUpdate($id)
  {
    $model=$this->loadModel($id);

    if(isset($_POST['AccommodationPhoto']))
    {
      $model->attributes=$_POST['AccommodationPhoto'];
      if($model->save())
        $this->redirect(array('list','accommodationId'=>$model->accommodation_id));
    }

    $this->render('update',array(
      'model'=>$model,
    ));
  }

  public function actionDelete($id)
  {
    if(Yii::app()->request->isPostRequest)
    {
      // we only allow deletion via POST request
      $model = $this->loadModel($id);
      
      Slide::model()->updateCounters(
        array('display_order'=> -1),
        'model_name=:modelName AND model_id=:modelId AND display_order >:display_order',
        array(':modelName' => $model->model_name, ':modelId' => $model->model_id , ':display_order' => $model->display_order)
      );
      $model->delete();

      // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
      if(!isset($_GET['ajax']))
        $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
    }
    else
      throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
  }

  public function actionSetDefault($id)
  {
    $model=$this->loadModel($id);

    $model->accommodation->default_photo_id = $id;

    if($model->accommodation->save())
      $this->redirect(array('list','accommodationId'=>$model->accommodation->id));
  }

  public function actionUpdateStatus($id)
  {
    $model = $this->loadModel($id);

    $model->enable = ($model->enable==true) ? 0 : 1;
    $model->save();
    $this->redirect(array("slide/list/modelName/$model->model_name/modelId/$model->model_id"));

  }

  public function actionUpPhoto($id)
  {
      $model = $this->loadModel($id);
      $display_order = $model->display_order-1;
      $prev_model = Slide::model()->find(array(
        'condition'=>'model_name=:model_name AND model_id=:model_id AND display_order=:display_order',
        'params'=>array(':model_name' => $model->model_name, ':model_id' => $model->model_id, ':display_order' => $display_order),
      ));
      $prev_model->display_order = $prev_model->display_order+1;

      $model->display_order = $display_order;
      $prev_model->save();
      $model->save();
      $this->redirect(array("slide/list/modelName/$model->model_name/modelId/$model->model_id"));
  }

  public function actionDownPhoto($id)
  {
      $model = $this->loadModel($id);
      $display_order = $model->display_order+1;

      $max_display_order = $model->getMaxDisplayOrder();

      $next_model = Slide::model()->find(array(
        'condition'=>'model_name=:model_name AND model_id=:model_id AND display_order=:display_order',
        'params'=>array(':model_name' => $model->model_name, ':model_id' => $model->model_id, ':display_order' => $display_order),
      ));
      if ($model->display_order < $max_display_order) {
        $next_model->display_order = $next_model->display_order-1;

        $model->display_order = $display_order;
        $next_model->save();
        $model->save();
      }
      $this->redirect(array("slide/list/modelName/$model->model_name/modelId/$model->model_id"));

  }


  public function loadModel($id)
  {
    $model=Slide::model()->findByPk($id);
    if($model===null)
      throw new CHttpException(404,'The requested page does not exist.');
    return $model;
  }


  protected function performAjaxValidation($model)
  {
    if(isset($_POST['ajax']) && $_POST['ajax']==='accommodation-photo-form')
    {
      echo CActiveForm::validate($model);
      Yii::app()->end();
    }
  }
}
